/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import dao.VacabularyDao;
import entity.Vacabulary;
import entity.Vocatype;
import entity.bean.Page;
import entity.bean.VocaTypeVo;

@Service
public class VocabularyService {

	@Resource
	private VacabularyDao vacabularyDao;

	public void saveVoca(Vacabulary vacabulary) {
		this.vacabularyDao.save(vacabulary);
	}

	public void deletevoca(long id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", Long.valueOf(id));
		this.vacabularyDao.executeSql("delete from vacabulary where id=:id",
				map);
	}

	public Vacabulary getVoca(int vocaid) {
		vocaid = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", Long.valueOf(1L));
		return ((Vacabulary) this.vacabularyDao.get(
				"from Vacabulary where id=:id", map));
	}

	public Page pageVoca(Page page) {
		page.setPagenum(10);
		if (page.getPageindex() == 0) {
			page.setPageindex(1);
		}
		page.setList(this.vacabularyDao.find("from Vacabulary",
				page.getPageindex(), page.getPagenum()));
		page.setTotalpage(this.vacabularyDao.countBySql(
				"select count(0) from vacabulary").intValue());
		return page;
	}

	public List<VocaTypeVo> getvocaType() {
		List<Object[]> list = this.vacabularyDao
				.findBySql("select vocatype.vocatypeid,vocatype.vocatypename,count(0) as 'count' from vacabulary inner join vocatype on vacabulary.vocatype=vocatype.vocatypeid group by vacabulary.vocatype");
		List<VocaTypeVo> vocatypevo = new ArrayList<VocaTypeVo>();
		VocaTypeVo vo = null;
		for (Object[] objects : list) {
			vo = new VocaTypeVo();
			for (int i = 0; i < objects.length; ++i) {
				vo.setVocatype(new Vocatype());
				vo.getVocatype().setVocatypeid(
						Integer.parseInt(objects[0].toString()));
				vo.getVocatype().setVocatypename(objects[1].toString());
				vo.setNum(Integer.parseInt(objects[2].toString()));
			}
			vocatypevo.add(vo);
		}

		return vocatypevo;
	}
}